﻿using System;
using System.Collections.Generic;
using NetServ.Net.Json;

namespace Ria.Infrastructure
{
    /// <summary>
    /// Class provides static methods for creating demo data,
    /// as well as demonstrates different ways of using NetServ
    /// </summary>
    public static class DemoDataServices
    {
        // this format for DateTime allows Flex to deserialize JSON date string
        // directly into Flex Date type
        internal static readonly string FlexDateFormat = "yyyy'/'MM'/'dd' 'HH':'mm':'ss";

        public static string GetControlOneInitialData()
        {
            // create some data
            var persons = new List<Person>
            {
                new Person {Name = "Will", Dob = new DateTime(1972,1,4), Kids = 1},
                new Person {Name = "Cole", Dob = new DateTime(1955,12,9), Kids = 0},
                new Person {Name = "Kira", Dob = new DateTime(1991,10,12), Kids = 2}
            };

            // serialize to json string
            var obj = new JsonObject();
            var people = new JsonArray();

            obj.Add("server_time", DateTime.Now.ToString(FlexDateFormat));
            foreach (var person in persons)
                people.Add(person.ToJsonObject());
            obj.Add("people", people);
            return obj.ToJsonString();
        }

        public static JsonObject GetSayHello()
        {
            return new JsonObject
                       {
                           { "action", "hello" },
                           { "message", "Hello from AJAX! Server time: " + DateTime.Now }
                       };
        }

        public static JsonObject GetOneRecord(byte index)
        {
            if (index == 0)
                return new JsonObject
                            {
                                {"action","add"},
                                {
                                    "record",
                                    new Person {Name="Michael", Dob = new DateTime(1968,11,17), Kids = 1}.ToJsonObject()
                                }
                           };
            return new JsonObject
                       {
                           {"action","add"},
                           {
                               "record",
                               new Person {Name = "Mary", Dob = new DateTime(1992,11,8), Kids = 3}.ToJsonObject()
                           }
                       };
        }

        public static JsonObject GetClear()
        {
            return new JsonObject{{"action", "clear"}};
        }
    }
}
